function getDefaultContent() {
  return {
    'application/json': {
      'schema': {
        type: 'object',
        example: {
          code: 200,
          data: {}
        }
      }
    },
  };
}
class Document {
  constructor() {
    this._method = undefined;
    this._path = undefined;
    this._tags = undefined;
    this._summary = undefined;
    this._description = undefined;
    this._requestBody = undefined;
    this._responses = undefined;
    this._parameters = undefined;

  }
  get method() {
    return this._method;
  }
  set method(m) {
    this._method = m;
  }
  get path() {
    return this._path;
  }
  set path(path) {
    this._path = path;
  }
  get summary() {
    return this._summary;
  }
  set summary(s) {
    this._summary = s;
  }
  get description() {
    return this._description;
  }
  set description(desc) {
    this._description = desc;
  }
  get tags() {
    return this._tags;
  }
  get requestBody() {
    return this._requestBody;
  }
  set requestBody(requestBody) {
    this._requestBody = requestBody;
  }
  get responses() {
    return this._responses;
  }
  get parameters() {
    return this._parameters;
  }
  addTag(tag) {
    if (this._tags === undefined) {
      this._tags = [tag];
    } else {
      this._tags.push(tag);
    }
  }
  addResponse(content, code = 200) {
    if (this._responses === undefined) {
      this._responses = {
        [code]: {
          description: content,
          content: getDefaultContent()
        }
      };
    } else {
      this._responses[code] = {
        description: content,
        content: getDefaultContent()
      };
    }
  }
  addParameters(params) {
    if (this._parameters === undefined) {
      this._parameters = [params];
    } else {
      this._parameters.push(params);
    }
  }
  toJson() {
    const content = {};
    const method = { [this.method]: content };
    const json = {
      [this.path]: method
    };
    if (this.tags) {
      content.tags = this.tags;
    }
    if (this.summary) {
      content.summary = this.summary;
    }
    if (this.description) {
      content.description = this.description;
    }
    if (this.parameters) {
      content.parameters = this.parameters;
    }
    if (this.requestBody) {
      content.requestBody = {
        content: {
          'application/json': {
            schema: this.buildSchema(this.requestBody)
          }
        }
      };
    }
    content.responses = {};
    if (this.responses) {
      content.responses = this.responses;
    } else {
      content.responses['200'] = {
        content: getDefaultContent()
      };
    }
    content.security = [{'Authorization': ''}];
    return json;
  }
  buildSchema(content) {
    const keys = Object.keys(content);
    const schema = {
      type: 'object',
      properties: {}
    };
    for (const key of keys) {
      const type = typeof content[key];
      schema.properties[key] = {
        type,
        example: content[key]
      };
    }
    return schema;
  }
}

module.exports = Document;